package com.example.view;

import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.app.MyApplication;
import com.example.netity.proinfo;
import com.example.tool.BaseActivity;
import com.example.tool.Utils;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements OnClickListener, Runnable {

    private TextView[] tv = new TextView[48];

    private TextView tvnum;
    private TextView tvline;
    private TextView id2, id3, id4, id5, id6, id7, po2;
    private TextView st2, st3, st4, st5, st6, st7;
    private TextView po3;
    private TextView po4;
    private TextView po5;
    private TextView po6;
    private TextView po7;
    private TextView zs2;
    private TextView zs3;
    private TextView zs4;
    private TextView zs5;
    private TextView zs6;
    private TextView zs7;
    private TextView wc2;
    private TextView wc3;
    private TextView wc4;
    private TextView wc5;
    private TextView wc6;
    private TextView wc7;
    private TextView add;
    private TextView tm;
    private TextView jian;
    private String chanxian, sn, renshu, k1, k2, k3, j1, j2, j3, hege, mbcl, po = "", wcs = "0", zs = "0", xnum = "0", xpph = "0", pph = "0", shike;
    private String k1_2, k2_2, j1_2, j2_2, k1_3, k2_3, j1_3, j2_3;
    private float lpph, sc;
    int nnn = 0;
    RequestQueue mQueue = null;
    JsonObjectRequest mJsonObjectRequest = null;
    String riqi = "", shijian = "";//
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss");
    private String url = MyApplication.SERVERPATH + "/Y502/Pad2getprinfo";
    private String logourl = MyApplication.SERVERPATH + "/Y502/Pad2getorder";
    private String surl = MyApplication.SERVERPATH + "/Y502/Insertdata";
    boolean l = true;
    private String tmstr = "";//条码
    private boolean start = true;//开始点击按钮或者刷卡  用于上传是否结束,防止多个上传
    int k = 0;//当前第几行
    int num = 0, n = 0, w = 0;//w是data的个数
    private String ohege, style, owcs;//上次的合格数与完成数
    private int ponum = 0; //订单的个数
    private boolean shoudao = true;//是否收到服务器的返回
    final Handler mHander = new Handler() {

        public void handleMessage(Message msg) {
            String s = (String) msg.obj;
            if (s.equals("3")) {
                setxianshi();

                getData();
            } else {
                add.setEnabled(false);
                jian.setEnabled(false);
                start = false;//sn="+sn+"&&pph="+pph+"&&hege="+hege+"&&po="+po+"&&num="+wcs+"&&shike="+shike +"&&xpph="+xpph+"&&xnum="+xnum+"&&tm="+tmstr;
                tmstr = s;
                tm.setText(s);
                ohege = hege;
                owcs = wcs;
                hege = Integer.parseInt(hege) + 1 + "";
                wcs = Integer.parseInt(wcs) + 1 + "";
                pph = String.format("%.2f", Integer.parseInt(hege) / sc / Integer.parseInt(renshu));
                if (tmcolor) {
                    tm.setBackgroundColor(Color.parseColor("#8A8400"));
                    tmcolor = false;
                } else {
                    tm.setBackgroundColor(Color.parseColor("#1a72b2"));
                    tmcolor = true;
                }
                long thistime = MyApplication.xtime * 1000 + System.currentTimeMillis();
                shijian = df1.format(new Date(thistime));
                n = 0;
                int selectkk = -1;
                for (k = 0; k < w; k++) {
                    String shijian2 = shijian.substring(0, 5);
                    String Shike2 = data.get(k).getShike2();//16:20
                    String Shike1 = data.get(k).getShike1();//16:00

                    if (k == 0 && shijian2.compareTo(Shike1) < 0) {
                        // 上班前扫码，统计到第一时段
                        selectkk = k;
                        break;
                    }

                    if (shijian2.compareTo(Shike1) >= 0 && shijian2.compareTo(Shike2) < 0) {
                        selectkk = k;
                        break;
                    }

                    if (k+1 < w) {
                        //中间休息时间
                        String Shike3 = data.get(k+1).getShike1();//下一个工作时段的起始时间
                        if (shijian2.compareTo(Shike2) >= 0 && shijian2.compareTo(Shike3) < 0) {
                            selectkk = k;
                            break;
                        }
                    } else {
                        //所有时段后，同时段内
                        selectkk = k;
                        break;
                    }
                }

                if (selectkk >= 0) {
                    if (data.get(selectkk).getHege().equals("")) {
                        n = 1;
                    } else {
                        n = Integer.parseInt(data.get(selectkk).getHege());
                        n++;
                    }
                    lpph = n / data.get(selectkk).getSc() / Float.parseFloat(renshu);
                    xpph = String.format("%.2f", lpph);
                    xnum = n + "";
                    shike = data.get(selectkk).getShike1();
                }

                if (k == w) {
                    add.setEnabled(true);
                    jian.setEnabled(true);
                    start = true;
                    hege = ohege;
                    wcs = owcs;
                    shoudao = true;
                    start = true;
                    return;
                }

                getdata3();

            }
        }
    };
    private LinearLayout line1;
    private LinearLayout line2;
    private LinearLayout line3;
    private LinearLayout line4;
    private LinearLayout line5;
    private LinearLayout line6;
    private LinearLayout line;
    private TextView t1, t2, t5, t3, t4;
    private TextView[] tt = new TextView[48];
    private proinfo p;//
    private ArrayList<proinfo> data = new ArrayList<proinfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        chanxian = getIntent().getStringExtra("chanxian");
        sn = getIntent().getStringExtra("sn");
        renshu = getIntent().getStringExtra("renshu");
        k1 = getIntent().getStringExtra("k1");
        k2 = getIntent().getStringExtra("k2");
        k3 = getIntent().getStringExtra("k3");
        j1 = getIntent().getStringExtra("j1");
        j2 = getIntent().getStringExtra("j2");
        j3 = getIntent().getStringExtra("j3");

        k1_2 = getIntent().getStringExtra("k1_2");
        k2_2 = getIntent().getStringExtra("k2_2");
        j1_2 = getIntent().getStringExtra("j1_2");
        j2_2 = getIntent().getStringExtra("j2_2");

        k1_3 = getIntent().getStringExtra("k1_3");
        k2_3 = getIntent().getStringExtra("k2_3");
        j1_3 = getIntent().getStringExtra("j1_3");
        j2_3 = getIntent().getStringExtra("j2_3");

        mbcl = getIntent().getStringExtra("mbcl");
        shike = "07:00";
        //hege=MyApplication.getSharedUserInfo().getString("nnn","0");
        long thistime = MyApplication.xtime * 1000 + System.currentTimeMillis();
        riqi = df.format(new Date(thistime));
        mQueue = MyApplication.getRequestQueue();
        initView();

        Shike00 = data.get(0).getShike1();
        getData();

        try {
            new Thread(this).start();
        } catch (Exception e) {
            e.printStackTrace();
        }

//        String end2 = "07:20";
//        end2 = data.get(0).getShike1().substring(2, 5);//:30
//        int a = 0;
    }

    String Shike00 = "";//每日开工时刻

    private void getData() {
        boolean isConnected = Utils.toastIsNetworkConnected(this);
        if (isConnected) {
            showProgressBar();
            String uu = url + "?riqi=" + riqi;
            //RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    hideProgressBar();
                    parseData(response);
                }


            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    hideProgressBar();
                    showToast(getString(R.string.network_error_try_again));
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            showToast(getString(R.string.network_error_try_again));
            return;
        }
    }

    public void parseData(final JSONObject response) {
        runOnUiThread(new Runnable() {
            public void run() {
                String re = null;
                try {
                    re = response.getString("result");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (re.equals("yes")) {
                    JSONArray array = response.optJSONArray("data");
                    if (array != null) {
                        nnn = 0;
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.optJSONObject(i);
                            String he = obj.optString("hege");
                            nnn = nnn + Integer.parseInt(he);
                            for (int k = 0; k < w; k++) {
                                if (data.get(k).getShike1().equals(obj.optString("shike"))) {
                                    data.get(k).setPph(obj.optString("pph"));
                                    data.get(k).setHege(obj.optString("hege"));
                                    break;
                                } else if (data.get(k).getShike1().compareTo(obj.optString("shike")) < 0 && data.get(k).getHege().equals("")) {
                                    data.get(k).setPph("0");
                                    data.get(k).setHege("0");
                                }
                            }

                        }
                        settable();
                        hege = nnn + "";
                        tvnum.setText(hege);
                    }
                }
            }
        });
        getdata2();
    }


    private void settable() {
        for (int i = 0; i < w; i++) {
            tv[i].setText(data.get(i).getHege());
            tv[i+24].setText(data.get(i).getPph());
        }
    }

    private void getdata2() {
        boolean isConnected = Utils.toastIsNetworkConnected(this);
        if (isConnected) {
            String uu = logourl + "?riqi=" + riqi;
            //RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    parseData2(response);
                    add.setEnabled(true);
                    jian.setEnabled(true);
                }


            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    add.setEnabled(true);
                    jian.setEnabled(true);
                    showToast(getString(R.string.network_error_try_again));
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            add.setEnabled(true);
            jian.setEnabled(true);
            showToast(getString(R.string.network_error_try_again));
            return;
        }
    }

    boolean _isgetdata3 = false;

    private void getdata3() {
        if (_isgetdata3) {
            add.setEnabled(true);
            jian.setEnabled(true);
            start = true;
            hege = ohege;
            wcs = owcs;
            shoudao = true;
            start = true;
            return;
        }
        boolean isConnected = Utils.toastIsNetworkConnected(this);
        if (isConnected) {
            pph = pph.replace(",", ".");
            xpph = xpph.replace(",", ".");
            try {
                po = URLEncoder.encode(po, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }/**/
            _isgetdata3 = true;
            String uu = surl + "?sn=" + sn + "&&pph=" + pph + "&&hege=" + hege + "&&po=" + po + "&&styel=" + style + "&&num=" + wcs + "&&shike=" + shike + "&&xpph=" + xpph + "&&xnum=" + xnum + "&&tm=" + tmstr;
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    parseData3(response);
                    _isgetdata3 = false;
                }


            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    _isgetdata3 = false;
                    showToast(getString(R.string.network_error_try_again));
                    add.setEnabled(true);
                    jian.setEnabled(true);
                    start = true;
                    hege = ohege;
                    wcs = owcs;
                    shoudao = true;
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            showToast(getString(R.string.network_error_try_again));
            add.setEnabled(true);
            jian.setEnabled(true);
            hege = ohege;
            wcs = owcs;
            start = true;
            return;
        }
    }

    protected void parseData3(JSONObject response) {
        String re = null;
        try {
//            add.setEnabled(true);
//            jian.setEnabled(true);
            start = true;
            re = response.getString("result");
            if (re.equals("ok")) {
                Message msg = mHander.obtainMessage();
                msg.obj = "3";
                mHander.sendMessage(msg);
            } else {
                hege = ohege;
                wcs = owcs;
                if (tmstr.equals("jian"))
                    num++;
                else
                    n--;
            }
//            Toast.makeText(MainActivity.this, re, Toast.LENGTH_SHORT).show();

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    protected void parseData2(final JSONObject response) {
        runOnUiThread(new Runnable() {
            public void run() {
                String re = null;
                try {
                    re = response.getString("result");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (re.equals("yes")) {
                    JSONArray array = response.optJSONArray("data");
                    if (array != null) {
                        ponum = array.length();
                        for (int i = 0; i < ponum; i++) {
                            JSONObject obj = array.optJSONObject(i);
                            if (i == 0) {
                                id2.setText("1");
                                String strPo = obj.optString("po");
                                po2.setText(obj.optString("po"));
                                st2.setText(obj.optString("style"));
                                zs2.setText(obj.optString("num"));
                                if (po.isEmpty()) {
                                    po = obj.optString("po");
                                    wcs = obj.optString("wnum");
                                } else if (po.compareToIgnoreCase(strPo) == 0) {
                                    wcs = obj.optString("wnum");
                                }
                                wc2.setText(obj.optString("wnum"));
                                style = obj.optString("style");
                                zs = obj.optString("num");
                            }
                            if (i == 1) {
                                String strPo = obj.optString("po");
                                if (po.compareToIgnoreCase(strPo) == 0) {
                                    wcs = obj.optString("wnum");
                                }
                                id3.setText("2");
                                po3.setText(obj.optString("po"));
                                st3.setText(obj.optString("style"));
                                zs3.setText(obj.optString("num"));
                                wc3.setText(obj.optString("wnum"));
                            }

                            if (i == 2) {
                                String strPo = obj.optString("po");
                                if (po.compareToIgnoreCase(strPo) == 0) {
                                    wcs = obj.optString("wnum");
                                }
                                id4.setText("3");
                                po4.setText(obj.optString("po"));
                                st4.setText(obj.optString("style"));
                                zs4.setText(obj.optString("num"));
                                wc4.setText(obj.optString("wnum"));
                            }
                            if (i == 3) {
                                String strPo = obj.optString("po");
                                if (po.compareToIgnoreCase(strPo) == 0) {
                                    wcs = obj.optString("wnum");
                                }
                                id5.setText("4");
                                po5.setText(obj.optString("po"));
                                st5.setText(obj.optString("style"));
                                zs5.setText(obj.optString("num"));
                                wc5.setText(obj.optString("wnum"));
                            }
                            if (i == 4) {
                                String strPo = obj.optString("po");
                                if (po.compareToIgnoreCase(strPo) == 0) {
                                    wcs = obj.optString("wnum");
                                }
                                id6.setText("5");
                                po6.setText(obj.optString("po"));
                                st6.setText(obj.optString("style"));
                                zs6.setText(obj.optString("num"));
                                wc6.setText(obj.optString("wnum"));
                            }
                            if (i == 5) {
                                String strPo = obj.optString("po");
                                if (po.compareToIgnoreCase(strPo) == 0) {
                                    wcs = obj.optString("wnum");
                                }
                                id7.setText("6");
                                po7.setText(obj.optString("po"));
                                st7.setText(obj.optString("style"));
                                zs7.setText(obj.optString("num"));
                                wc7.setText(obj.optString("wnum"));
                            }
                        }
					     /*if(array.length()==0)
					      {
					    	  id2.setText("1");
					          po2.setText("123456789");
					          zs2.setText(mbcl);
					          wc2.setText(hege);
					          po="123456789";
					          wcs=hege;
					          zs=mbcl;
					          wc2.setText(hege);
					      }*/
                    }
                }
            }
        });

    }

    private void settime(String k1, String j1) {
        int a, b, c, d, k = data.size();//k是数量
        int n = k;
        String s1, s2, ss;
        a = Integer.parseInt(k1.substring(0, 2));
        b = Integer.parseInt(k1.substring(3, 5));
        c = Integer.parseInt(j1.substring(0, 2));
        d = Integer.parseInt(j1.substring(3, 5));
        s1 = k1.substring(3, 5);
        s2 = j1.substring(3, 5);
        if (c <  a) {
            c += 24;
        }
        while (c >= a) {
//            if (k > 10) {
//                data.get(k - 1).setShike2(j1);
//                data.get(k - 1).setSc((float) ((c - a + 1) * 60 + (d - b)) / 60);
//                tt[2 * k - 1].setText(j1);
//                break;
//            }
            if (c > a) {
                int aa = a;
                if (aa >= 24) {
                    aa -= 24;
                }
                ss = aa < 10 ? "0" + aa : aa + "";
                if (k >= n + 1) {
                    data.get(k - 1).setShike2(ss + ":" + s1);
                    data.get(k - 1).setSc(1);
                    tt[2 * k - 1].setText(ss + ":" + s1);
                }
                String ssk = ss + ":" + s1;
                tt[2 * k].setText(ssk);
                p = new proinfo();
                p.setShike1(ss + ":" + s1);
                data.add(p);
                k++;
                a++;
            } else {
                ss = a < 10 ? "0" + a : a + "";
                if (s2.equals("00") || b >= d) {
                    data.get(k - 1).setShike2(j1);
                    tt[2 * k - 1].setText(j1);
                    data.get(k - 1).setSc((float) (60 + d - b) / 60);
                    break;
                } else {
                    if (k < n + 1) {
                        p = new proinfo();
                        p.setShike1(k1);
                        p.setShike2(j1);
                        p.setSc((float) (d - b) / 60);
                        data.add(p);
                        tt[2 * k].setText(k1);
                        tt[2 * k + 1].setText(j1);
                        break;
                    } else {
                        data.get(k - 1).setShike2(ss + ":" + s1);
                        data.get(k - 1).setSc(1);
                        ;
                        tt[2 * k - 1].setText(ss + ":" + s1);
                        p = new proinfo();
                        p.setShike1(ss + ":" + s1);
                        p.setShike2(j1);
                        p.setSc((float) (d - b) / 60);
                        data.add(p);
                        tt[2 * k].setText(ss + ":" + s1);
                        tt[2 * k + 1].setText(j1);
                        break;
                    }
                }
            }
        }
    }

    private void settime1() {
        if (k1.equals("00:00")) {
            k1 = "07:00";
            j1 = "17:00";
        }
        settime(k1, j1);
        if (!k2.equals("00:00"))
            settime(k2, j2);
        if (!k3.equals("00:00"))
            settime(k3, j3);

//        if (k1_2.isEmpty()) {
//            k1_2 = "14:00";
//            j1_2 = "18:00";
//
//            k2_2 = "19:00";
//            j2_2 = "23:00";
//        }
//
        if (!k1_2.equals(j1_2)) {
            settime(k1_2, j1_2);
        }
        if (!k2_2.equals(j2_2)) {
            settime(k2_2, j2_2);
        }

        if (!k1_3.equals(j1_3)) {
            settime(k1_3, j1_3);
        }
        if (!k2_3.equals(j2_3)) {
            settime(k2_3, j2_3);
        }

    }

    private float cha() {
        float f = 0;
        w = data.size();
        for (int i = 0; i < w; i++) {
            f += data.get(i).getSc();
        }
        return f;
    }

    private LinearLayout mGallery;
    private LayoutInflater mInflater;

    private void initView() {
        mInflater = LayoutInflater.from(this);
        mGallery = (LinearLayout) findViewById(R.id.id_gallery);

        for (int i = 0; i < 24; i++) {
            View view = mInflater.inflate(R.layout.item_gallery, mGallery, false);
            tt[i*2] = (TextView) view.findViewById(R.id.t1);//时段开始时刻
            tt[i*2+1] = (TextView) view.findViewById(R.id.t2);//时段结束时刻
            tv[i] = (TextView) view.findViewById(R.id.t3);//时段i 产量
            tv[24 + i] = (TextView) view.findViewById(R.id.t4);//时段i PPH
            mGallery.addView(view);
        }
        settime1();
        sc = cha();
        tvnum = (TextView) findViewById(R.id.main_tvnum);

        tvnum.setOnClickListener(this);
        tvline = (TextView) findViewById(R.id.main_tvline);
        tvline.setText(chanxian);
        id2 = (TextView) findViewById(R.id.main_tvid2);
        id3 = (TextView) findViewById(R.id.main_tvid3);
        id4 = (TextView) findViewById(R.id.main_tvid4);
        id5 = (TextView) findViewById(R.id.main_tvid5);
        id6 = (TextView) findViewById(R.id.main_tvid6);
        id7 = (TextView) findViewById(R.id.main_tvid7);
        po2 = (TextView) findViewById(R.id.main_tvpo2);
        po3 = (TextView) findViewById(R.id.main_tvpo3);
        po4 = (TextView) findViewById(R.id.main_tvpo4);
        po5 = (TextView) findViewById(R.id.main_tvpo5);
        po6 = (TextView) findViewById(R.id.main_tvpo6);
        po7 = (TextView) findViewById(R.id.main_tvpo7);
        st2 = (TextView) findViewById(R.id.main_tvst2);
        st3 = (TextView) findViewById(R.id.main_tvst3);
        st4 = (TextView) findViewById(R.id.main_tvst4);
        st5 = (TextView) findViewById(R.id.main_tvst5);
        st6 = (TextView) findViewById(R.id.main_tvst6);
        st7 = (TextView) findViewById(R.id.main_tvst7);
        zs2 = (TextView) findViewById(R.id.main_tvzs2);
        zs3 = (TextView) findViewById(R.id.main_tvzs3);
        zs4 = (TextView) findViewById(R.id.main_tvzs4);
        zs5 = (TextView) findViewById(R.id.main_tvzs5);
        zs6 = (TextView) findViewById(R.id.main_tvzs6);
        zs7 = (TextView) findViewById(R.id.main_tvzs7);
        wc2 = (TextView) findViewById(R.id.main_tvwc2);
        wc3 = (TextView) findViewById(R.id.main_tvwc3);
        wc4 = (TextView) findViewById(R.id.main_tvwc4);
        wc5 = (TextView) findViewById(R.id.main_tvwc5);
        wc6 = (TextView) findViewById(R.id.main_tvwc6);
        wc7 = (TextView) findViewById(R.id.main_tvwc7);
        tm = (TextView) findViewById(R.id.main_tvtm);
        add = (TextView) findViewById(R.id.main_tvadd);
        jian = (TextView) findViewById(R.id.main_tvjian);
        line1 = (LinearLayout) findViewById(R.id.main_line1);
        line2 = (LinearLayout) findViewById(R.id.main_line2);
        line3 = (LinearLayout) findViewById(R.id.main_line3);
        line4 = (LinearLayout) findViewById(R.id.main_line4);
        line5 = (LinearLayout) findViewById(R.id.main_line5);
        line6 = (LinearLayout) findViewById(R.id.main_line6);
        add.setOnClickListener(this);
        jian.setOnClickListener(this);
        line1.setOnClickListener(this);
        line2.setOnClickListener(this);
        line3.setOnClickListener(this);
        line4.setOnClickListener(this);
        line5.setOnClickListener(this);
        line6.setOnClickListener(this);
        line = line1;
        t1 = id2;
        t2 = po2;
        t3 = zs2;
        t4 = wc2;
        t5 = st2;
        t1.setBackgroundColor(Color.parseColor("#8A8400"));
        t2.setBackgroundColor(Color.parseColor("#8A8400"));
        t3.setBackgroundColor(Color.parseColor("#8A8400"));
        t4.setBackgroundColor(Color.parseColor("#8A8400"));
        t5.setBackgroundColor(Color.parseColor("#8A8400"));
    }

    public void setxianshi() {
        if (k >= data.size()) {
            return;
        }

//        if (tmstr.equals("jian")) {
//            data.get(k).setPph(xpph);
//            data.get(k).setHege(num + "");
//            tv[k + 1].setText(num + "");
//            tv[k + 24].setText(xpph);
//        } else {
//            data.get(k).setHege(n + "");
//            data.get(k).setPph(xpph);
//            tv[k + 1].setText(n + "");
//            tv[k + 24].setText(xpph);
//        }
//        tvnum.setText(hege);
//        if (line == line1) {
//            wc2.setText(wcs);
//
//        } else if (line == line2) {
//            wc3.setText(wcs);
//        } else if (line == line3) {
//            wc4.setText(wcs);
//        } else if (line == line4) {
//            wc5.setText(wcs);
//        } else if (line == line5) {
//            wc6.setText(wcs);
//        } else if (line == line6) {
//            wc7.setText(wcs);
//        }
//        if (po.equals("esm"))
//            po = "123456789";
        shoudao = true;
    }

    boolean tmcolor = true;

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.main_tvadd: {
//                if (Utils.isFastClick()) {
//                    return;
//                }

                if (!shoudao || ponum <= 0)
                    return;
                add.setEnabled(false);
                jian.setEnabled(false);
                start = false;
                tmstr = "add";
                ohege = hege;
                owcs = wcs;
                hege = Integer.parseInt(hege) + 1 + "";
                wcs = Integer.parseInt(wcs) + 1 + "";
                pph = String.format("%.2f", Integer.parseInt(hege) / sc / Integer.parseInt(renshu));
                long thistime = MyApplication.xtime * 1000 + System.currentTimeMillis();
                shijian = df1.format(new Date(thistime));
//                String sj = shijian.substring(0, 2) + ":00";

//                shijian = "23:40:10";//测试

                n = 0;
                if (tmcolor) {
                    tm.setBackgroundColor(Color.parseColor("#8A8400"));
                    tmcolor = false;
                } else {
                    tm.setBackgroundColor(Color.parseColor("#1a72b2"));
                    tmcolor = true;
                }
                int selectkk = -1;
                for (k = 0; k < w; k++) {
                    String shijian2 = shijian.substring(0, 5);
                    String Shike2 = data.get(k).getShike2();//16:20
                    String Shike1 = data.get(k).getShike1();//16:00
                    if (shijian2.compareTo(Shike1) >= 0 && shijian2.compareTo(Shike2) < 0) {
                        selectkk = k;
                        break;
                    }

                    if (k+1 < w) {
                        //中间休息时间
                        String Shike3 = data.get(k+1).getShike1();//下一个工作时段的起始时间
                        if (shijian2.compareTo(Shike2) >= 0 && shijian2.compareTo(Shike3) < 0) {
                            selectkk = k;
                            break;
                        }
                    } else {
                        //所有时段后，同时段内
                        selectkk = k;
                        break;
                    }
                }

                if (selectkk >= 0) {
                    if (data.get(selectkk).getHege().equals("")) {
                        n = 1;
                    } else {
                        n = Integer.parseInt(data.get(selectkk).getHege());
                        n++;
                    }
                    lpph = n / data.get(selectkk).getSc() / Float.parseFloat(renshu);
                    xpph = String.format("%.2f", lpph);
                    xnum = n + "";
                    shike = data.get(selectkk).getShike1();
                }

                if (k == w) {
                    add.setEnabled(true);
                    jian.setEnabled(true);
                    start = true;
                    hege = ohege;
                    wcs = owcs;
                    shoudao = true;
                    start = true;
                    Toast.makeText(MainActivity.this, "no", Toast.LENGTH_SHORT).show();
                    return;
                }
                shoudao = false;
                getdata3();
            }
            break;
            case R.id.main_tvjian: {
//                if (Utils.isFastClick()) {
//                    return;
//                }

                if (Integer.parseInt(wcs) == 0) {

                } else {
                    if (!shoudao || ponum <= 0)
                        return;
                    add.setEnabled(false);
                    jian.setEnabled(false);
                    start = false;
                    tmstr = "jian";
                    ohege = hege;
                    owcs = wcs;
                    hege = Integer.parseInt(hege) - 1 + "";
                    wcs = Integer.parseInt(wcs) - 1 + "";
                    pph = String.format("%.2f", Integer.parseInt(hege) / sc / Integer.parseInt(renshu));
                    long thistime = MyApplication.xtime * 1000 + System.currentTimeMillis();
                    shijian = df1.format(new Date(thistime));
                    String sj = shijian.substring(0, 5);
                    if (tmcolor) {
                        tm.setBackgroundColor(Color.parseColor("#8A8400"));
                        tmcolor = false;
                    } else {
                        tm.setBackgroundColor(Color.parseColor("#1a72b2"));
                        tmcolor = true;
                    }
                    num = 0;
                    for (k = w - 1; k >= 0; k--) {
                        if (data.get(k).getShike1().compareTo(sj) <= 0) {
                            if (!data.get(k).getHege().equals("")) {
                                num = Integer.parseInt(data.get(k).getHege());
                                if (num > 0) {
                                    num--;
                                    shike = data.get(k).getShike1();
                                    lpph = num / data.get(k).getSc() / Float.parseFloat(renshu);
                                    xpph = String.format("%.2f", lpph);
                                    xnum = num + "";
                                    break;
                                }
                            }
                        }
                    }
                    shoudao = false;
                    getdata3();
                }
            }
            break;
            case R.id.main_line1: {
                if (line != line1) {
                    t1.setBackgroundResource(R.drawable.r1);
                    t2.setBackgroundResource(R.drawable.r2);//.setBackgroundColor(R.drawable.r2);
                    t5.setBackgroundResource(R.drawable.r3);
                    t3.setBackgroundResource(R.drawable.r3);
                    t4.setBackgroundResource(R.drawable.r4);
                    t1 = id2;
                    t2 = po2;
                    t5 = st2;
                    t3 = zs2;
                    t4 = wc2;
                    t1.setBackgroundColor(Color.parseColor("#8A8400"));
                    t2.setBackgroundColor(Color.parseColor("#8A8400"));
                    t5.setBackgroundColor(Color.parseColor("#8A8400"));
                    t3.setBackgroundColor(Color.parseColor("#8A8400"));
                    t4.setBackgroundColor(Color.parseColor("#8A8400"));

                    line = line1;
                    po = po2.getText().toString();
                    style = st2.getText().toString();
                    zs = zs2.getText().toString();
                    wcs = wc2.getText().toString();
                }
            }
            break;
            case R.id.main_line2:
                if (line != line2 && !po3.getText().toString().equals("")) {
                    t1.setBackgroundResource(R.drawable.r1);
                    t2.setBackgroundResource(R.drawable.r2);
                    t5.setBackgroundResource(R.drawable.r3);
                    t3.setBackgroundResource(R.drawable.r3);
                    t4.setBackgroundResource(R.drawable.r4);
                    t1 = id3;
                    t2 = po3;
                    t5 = st3;
                    t3 = zs3;
                    t4 = wc3;
                    t1.setBackgroundColor(Color.parseColor("#8A8400"));
                    t2.setBackgroundColor(Color.parseColor("#8A8400"));
                    t5.setBackgroundColor(Color.parseColor("#8A8400"));
                    t3.setBackgroundColor(Color.parseColor("#8A8400"));
                    t4.setBackgroundColor(Color.parseColor("#8A8400"));
                    line = line2;
                    po = po3.getText().toString();
                    style = st3.getText().toString();
                    zs = zs3.getText().toString();
                    wcs = wc3.getText().toString();
                }
                break;
            case R.id.main_line3:
                if (line != line3 && !po4.getText().toString().equals("")) {
                    t1.setBackgroundResource(R.drawable.r1);
                    t2.setBackgroundResource(R.drawable.r2);//.setBackgroundColor(R.drawable.r2);
                    t5.setBackgroundResource(R.drawable.r3);
                    t3.setBackgroundResource(R.drawable.r3);
                    t4.setBackgroundResource(R.drawable.r4);
                    t1 = id4;
                    t2 = po4;
                    t5 = st4;
                    t3 = zs4;
                    t4 = wc4;
                    t1.setBackgroundColor(Color.parseColor("#8A8400"));
                    t2.setBackgroundColor(Color.parseColor("#8A8400"));
                    t5.setBackgroundColor(Color.parseColor("#8A8400"));
                    t3.setBackgroundColor(Color.parseColor("#8A8400"));
                    t4.setBackgroundColor(Color.parseColor("#8A8400"));
                    line = line3;
                    po = po4.getText().toString();
                    style = st4.getText().toString();
                    zs = zs4.getText().toString();
                    wcs = wc4.getText().toString();
                }
                break;
            case R.id.main_line4:
                if (line != line4 && !po5.getText().toString().equals("")) {
                    t1.setBackgroundResource(R.drawable.r1);
                    t2.setBackgroundResource(R.drawable.r2);//.setBackgroundColor(R.drawable.r2);
                    t5.setBackgroundResource(R.drawable.r3);
                    t3.setBackgroundResource(R.drawable.r3);
                    t4.setBackgroundResource(R.drawable.r4);
                    t1 = id5;
                    t2 = po5;
                    t5 = st5;
                    t3 = zs5;
                    t4 = wc5;
                    t1.setBackgroundColor(Color.parseColor("#8A8400"));
                    t2.setBackgroundColor(Color.parseColor("#8A8400"));
                    t5.setBackgroundColor(Color.parseColor("#8A8400"));
                    t3.setBackgroundColor(Color.parseColor("#8A8400"));
                    t4.setBackgroundColor(Color.parseColor("#8A8400"));
                    line = line4;
                    po = po5.getText().toString();
                    style = st5.getText().toString();
                    zs = zs5.getText().toString();
                    wcs = wc5.getText().toString();
                }
                break;
            case R.id.main_line5:
                if (line != line5 && !po6.getText().toString().equals("")) {
                    t1.setBackgroundResource(R.drawable.r1);
                    t2.setBackgroundResource(R.drawable.r2);//.setBackgroundColor(R.drawable.r2);
                    t5.setBackgroundResource(R.drawable.r3);
                    t3.setBackgroundResource(R.drawable.r3);
                    t4.setBackgroundResource(R.drawable.r4);
                    t1 = id6;
                    t2 = po6;
                    t5 = st6;
                    t3 = zs6;
                    t4 = wc6;
                    t1.setBackgroundColor(Color.parseColor("#8A8400"));
                    t2.setBackgroundColor(Color.parseColor("#8A8400"));
                    t5.setBackgroundColor(Color.parseColor("#8A8400"));
                    t3.setBackgroundColor(Color.parseColor("#8A8400"));
                    t4.setBackgroundColor(Color.parseColor("#8A8400"));
                    line = line5;
                    po = po6.getText().toString();
                    style = st6.getText().toString();
                    zs = zs6.getText().toString();
                    wcs = wc6.getText().toString();
                }
                break;
            case R.id.main_line6:
                if (line != line6 && !po7.getText().toString().equals("")) {
                    t1.setBackgroundResource(R.drawable.r1);
                    t2.setBackgroundResource(R.drawable.r2);//.setBackgroundColor(R.drawable.r2);
                    t5.setBackgroundResource(R.drawable.r3);
                    t3.setBackgroundResource(R.drawable.r3);
                    t4.setBackgroundResource(R.drawable.r4);
                    t1 = id7;
                    t2 = po7;
                    t5 = st7;
                    t3 = zs7;
                    t4 = wc7;
                    t1.setBackgroundColor(Color.parseColor("#8A8400"));
                    t2.setBackgroundColor(Color.parseColor("#8A8400"));
                    t5.setBackgroundColor(Color.parseColor("#8A8400"));
                    t3.setBackgroundColor(Color.parseColor("#8A8400"));
                    t4.setBackgroundColor(Color.parseColor("#8A8400"));
                    line = line6;
                    po = po7.getText().toString();
                    style = st7.getText().toString();
                    zs = zs7.getText().toString();
                    wcs = wc7.getText().toString();
                }
                break;
            case R.id.main_tvnum: {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("sn", sn);
                startActivity(intent);
            }
            break;
        }
    }


    @Override
    public void run() {
        try {
            byte buf[] = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf, 1024);
            DatagramSocket datagramSocket = new DatagramSocket(9091);
            while (true) {
                if (start) {
                    datagramSocket.receive(dp);
                    String msg1 = new String(buf, 0, dp.getLength());
                    if (msg1.length() > 1 && shoudao && ponum > 0) {
                        shoudao = false;
                        msg1.replace("!", "");
                        Message msg = mHander.obtainMessage();
                        msg.obj = msg1;
                        mHander.sendMessage(msg);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
